//
//  CategoryLabelsView.m
//  CToolBox
//
//  Created by crj on 16/8/10.
//  Copyright © 2016年 oyf. All rights reserved.
//

#import "CategoryLabelsView.h"
#import "UIView+Frame.h"
#import "UIImage+compressIMG.h"


@interface CategoryLabelsView()
{
     CGFloat _selfHeight;
     CGFloat _selfWidth;
     int _section;//行数
     CGFloat _lastWidth;//最后一个label的right
}

@property (nonatomic, strong) UIImage *selectedImage;


@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSMutableArray *labelArray;



@property (nonatomic, strong) NSMutableArray *selectTitles;//选中的标题(多选时才用)

@end

@implementation CategoryLabelsView

static CGFloat const labelHeight = 25;//label高度
static CGFloat const minWidth = 77;//label最小宽度
static CGFloat const spaceSide = 10; //边距
static CGFloat const space = 5;//间距

- (void)setConfig{
    _isSingleSelect = YES;
    _selectedImage = [UIImage createImageWithColor:HEXCOLOR(kColorBlue5, 1)];
    _selectTitles = [[NSMutableArray alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame labels:(NSArray *)labels target:(id)target isSingleSelect:(BOOL)isSingleSelect selectTitles:(NSArray *)selectTitles{
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setConfig];
        
        _isSingleSelect = isSingleSelect;
        _selfWidth = frame.size.width;
        [_selectTitles addObjectsFromArray:selectTitles];
        
        _titles = labels;
        _delegate = target;
        if(_titles){
            [self initLabelsWithArray:_titles];
        }
        frame.size.height = _selfHeight;
        self.frame = frame;
    }
    return self;
}


- (instancetype)initWithFrame:(CGRect)frame labels:(NSArray *)labels target:(id)target
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setConfig];

        _selfWidth = frame.size.width;
        
        _titles = labels;
        _delegate = target;
        if(_titles){
            [self initLabelsWithArray:_titles];
        }
        frame.size.height = _selfHeight;
        self.frame = frame;
    }
    return self;
}

- (void)setInitVaule{
    
    _section = 0;
    _lastWidth = 0;
}

- (void)drawRect:(CGRect)rect {


}

- (void)layoutSubviews{
    
    [super layoutSubviews];

}


//初始化多选状态
- (void)initDataWithSelectTitles{
    
    for(UIButton *btn in _labelArray){
        
        btn.selected = [_selectTitles containsObject:btn.titleLabel.text];
        
    }
}

- (void)initLabelsWithArray:(NSArray *)labels{
   
    [self setInitVaule];
    
    for(UIView *view in self.subviews){
        [view removeFromSuperview];
    }
    
    _labelArray = [[NSMutableArray array] init];
    
    for(int i = 0; i<labels.count; i++){
        
        UIFont *font = [UIFont systemFontOfSize:13];
        CGFloat labelWidth = [self returnLabelWidthWithString:labels[i] font:font];
        CGFloat x = [self returnXWithneedWidth:labelWidth];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        button.frame = CGRectMake(x, _section*(labelHeight+space), labelWidth, labelHeight);
        button.titleLabel.font = font;
        button.layer.masksToBounds = YES;
        button.layer.borderColor = HEXCOLOR(kColorBlue5, 1).CGColor;
        button.layer.borderWidth = 1;
        button.layer.cornerRadius = kCornerRadius;
        [button setTitle:labels[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(categoryAction:) forControlEvents:UIControlEventTouchUpInside];
       
        
        [button setTitleColor:HEXCOLOR(kColorBlue5, 1) forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button setBackgroundColor:[UIColor clearColor]];
        [button setBackgroundImage:_selectedImage forState:UIControlStateSelected];
        
        [self addSubview:button];
        
        _lastWidth = button.yj_right;
        _selfHeight = button.yj_bottom;
        
        [_labelArray addObject:button];
    }
    
    if(!_isSingleSelect){
        [self initDataWithSelectTitles];
    }
}

- (void)reloadStateWithSelectTitles:(NSArray *)selectTitles{
    
    [_selectTitles removeAllObjects];
    [_selectTitles addObjectsFromArray:selectTitles];
    
    [self initDataWithSelectTitles];
}

- (void)categoryAction:(UIButton *)btn{
    
    if(_isSingleSelect){
        for(UIButton *button in _labelArray){
            button.selected = NO;
        }
        btn.selected = YES;
        
        if([_delegate respondsToSelector:@selector(categoryActionWithIndex:)]){
            [_delegate categoryActionWithIndex:btn.tag];
        }
    }else{
        btn.selected = !btn.selected;
        if(btn.selected){
            [_selectTitles addObject:btn.titleLabel.text];
        }else{
            [_selectTitles removeObject:btn.titleLabel.text];

        }
        if([_delegate respondsToSelector:@selector(categoryActionWithTitles:)]){
            [_delegate categoryActionWithTitles:_selectTitles];
        }
    }
}
/**
 *  返回当前label的x
 *
 *  @param lastWidth 当前行的最后一个label的bottom
 *  @param needWidth 当前label 的宽度
 *
 *  @return 当前label的x
 */
- (CGFloat)returnXWithneedWidth:(CGFloat)needWidth{
    
    CGFloat needAllWidth = _lastWidth+needWidth + spaceSide*2;
    CGFloat x = 0;
    if(needAllWidth>_selfWidth){
        _section++;
        _lastWidth = 0;
        return x+spaceSide;
    }else{
        if(_section == 0 && _lastWidth == 0){
            return _lastWidth+spaceSide;
        }else{
            return _lastWidth+space;
        }
    }
    
}

//返回宽度
- (CGFloat)returnLabelWidthWithString:(NSString *)string font:(UIFont *)font
{
    //至少需要的宽度
    CGFloat needWidth = [Utils widthOfString:string font:font height:20]+20;
    
    return needWidth>minWidth?needWidth:minWidth;
    
}

#pragma mark - SET

- (void)setSelectIndex:(NSInteger)selectIndex{
    
    _selectIndex = selectIndex;
    
    UIButton *btn = _labelArray[_selectIndex];
    [self categoryAction:btn];

}



@end
